import BTreeTools from "../BTreeTools";
import BTree from "./BTree";
import {ActionResult} from "../BTreeActionResult";

export default class BNode extends cc.Component{
    public bTree: BTree = null;
    protected m_type: string;
    protected m_name: string;
    private m_eState: ActionResult = ActionResult.NONE;
    protected m_cParent: BNode;  //父节点
    protected m_lstChildren: Array<BNode> = new Array<BNode>();   //子节点
    protected parameter: any;
    protected _errorMessage: string;
    public set errorMessage(value: string) {
        this._errorMessage = value;
    }

    public get errorMessage(): string {
        return this.className + ":" + this._errorMessage;
    }

    protected get className(): void {
        return this["__proto__"]["constructor"].name;
    }

    public readJson(jsonData: any): void {
        this.m_type = jsonData["type"];
        this.m_name = jsonData["name"];
        this.parameter = JSON.parse(jsonData["parameter"]);
        if (jsonData["childs"] && jsonData["childs"].length > 0) {
            let nodeArray: Array<any> = jsonData["childs"];
            for (let i = 0; i < nodeArray.length; i++) {
                let node: BNode = BTreeTools.instance.getNewBNode(nodeArray[i]["type"]);
                node.m_cParent = this;
                node.bTree = this.bTree;
                node.readJson(nodeArray[i]);
                this.addChild(node);
            }
        }
    }

    public runNode(): ActionResult {
        if (this.m_eState == ActionResult.NONE) {
            this.enter();
            this.m_eState = ActionResult.RUNNING;
        }
        let res: ActionResult = this.excute();
        if (res != ActionResult.RUNNING) {
            this.exit();
            this.m_eState = ActionResult.NONE;
        }
        return res;
    }

    public enter(): void {
        this.onEnter();
    }

    public excute(): ActionResult {
        return this.onExcute();

    }

    public exit(): void {
        this.onExit();
    }

    protected onEnter(): void {

    }

    protected onExcute(): ActionResult {
        console.log("type:" + this.m_type + " -name:" + this.m_name + " -parameter:" + this.parameter);
        return ActionResult.SUCCESS;
    }

    protected onExit(): void {

    }

    public addChild(node: BNode): void {
        this.m_lstChildren.push(node);
    }

    public removeChild(node: BNode): void {
        let index = this.m_lstChildren.indexOf(node);
        if (index > -1) {
            this.m_lstChildren.splice(index, 1);
        }
    }

    public containChild(node: BNode): boolean {
        return this.m_lstChildren.indexOf(node) > -1;
    }

    /**
     * 获取共享变量
     * @param {string} variableName
     * @returns {T}
     */
    protected getVariable<T>(variableName: string): T {
        return this.bTree.getVariable<T>(variableName);
    }

    /**
     * 设置共享变量
     * @param {string} variableName
     * @param value
     */
    protected setVariable(variableName: string, value: any): void {
        this.bTree.setVariable(variableName, value);
    }

    /**
     * 删除共享变量
     * @param {string} variableName
     */
    protected deleteVariable(variableName: string): void {
        this.bTree.deleteVariable(variableName);
    }

    /**
     * 获取行为树参数
     * @param {string} name
     * @returns {any}
     */
    protected getParameter<T>(name: string): T {
        return this.parameter[name];
    }
}
